﻿using System;
using System.IO;
using System.Web;
using System.Net;
namespace BIN
{
    public static  class WebOp
    {
        public static void  Download(string url, string fileName,  string Path)
        {
            if (!(Directory.Exists(Path))) Directory.CreateDirectory(Path);
            string serverUrl = url;
            if (!(serverUrl.EndsWith(@"/"))) serverUrl += @"/";
            if (!(Path.EndsWith(@"\"))) Path += @"\";
            Path = Common.CombineDir(Path , fileName );
            if (System.IO.File.Exists(Path))
            {
                try
                {
                    System.IO.File.Delete(Path);
                }
                catch { }
            }
            serverUrl += fileName;
            WebClient  req = null;
            try
            {
                req = new WebClient();
                if (!serverUrl.StartsWith("ftp:", StringComparison.OrdinalIgnoreCase))
                {
                    req.UseDefaultCredentials = true;
                }
                Common.CheckDownUrl(ref serverUrl, ref Path);
                req.DownloadFile(serverUrl, Path); 
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (req != null) req.Dispose();
            }
        }
    }
}
